import React from 'react';

class ErrorBoundary extends React.Component {
    constructor(props) {
      super(props);
      this.state = { hasError: false };
    }
  
    static getDerivedStateFromError(error) {
        console.log('error',error)
        // 更新 state 使下一次渲染能够显示降级后的 UI
        return { hasError: true };
    }
  
    componentDidCatch(error, errorInfo) {
        console.log('error',error);
        console.log('errorInfo',errorInfo)
        // 你同样可以将错误日志上报给服务器
        //logErrorToMyService(error, errorInfo);
    }
  
    render() {
        console.log('this.state.hasError',this.state.hasError)
        if (this.state.hasError) {
            // 你可以自定义降级后的 UI 并渲染
            return <h1>Something went wrong.</h1>;
        }
    
        return this.props.children;
    }
}

export default ErrorBoundary;